/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.order.rest;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.order.domain.PayOrder;
import me.zhengjie.modules.order.domain.req.OrderDetailReq;
import me.zhengjie.modules.order.domain.req.RecordOrderReq;
import me.zhengjie.modules.order.domain.resp.OrderDetailResp;
import me.zhengjie.modules.order.domain.resp.OrderListResp;
import me.zhengjie.modules.order.domain.resp.OrderPreviewResp;
import me.zhengjie.modules.order.service.PayOrderService;
import me.zhengjie.modules.order.domain.vo.PayOrderQueryCriteria;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.pay.domain.req.BuyProductReq;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
 * @author yiwei.he
 * @date 2024-03-29
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "订单表管理")
@RequestMapping("/api/payOrder")
@Slf4j
public class PayOrderController {

    private final PayOrderService payOrderService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check()")
    public void exportPayOrder(HttpServletResponse response, PayOrderQueryCriteria criteria) throws IOException {
        payOrderService.download(payOrderService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询订单列表")
    @ApiOperation("查询订单列表")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<PageResult<PayOrder>> queryPayOrder(PayOrderQueryCriteria criteria, Page<Object> page){
        log.info("/api/payOrder 查询订单列表(后管)：{}，分页参数：{}",criteria,page);
        return new ResponseEntity<>(payOrderService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping("/list")
    @Log("查询订单列表（小程序）")
    @ApiOperation("查询订单列表（小程序）")
    public ResponseEntity<PageResult<OrderListResp>> list(@RequestBody OrderDetailReq req){
        log.info("/api/payOrder/list 查询订单列表(小程序)：{}",req);
        return new ResponseEntity<>(payOrderService.list(req,req.getPage()),HttpStatus.OK);
    }

    @PostMapping("/create")
    @Log("创建订单")
    @ApiOperation("创建订单")
    public ResponseEntity<String> createOrder(@Validated @RequestBody BuyProductReq req) throws BizException {
        log.info("/api/payOrder/create 创建订单：{}",req);
        return new ResponseEntity<>(payOrderService.createOrder(req), HttpStatus.OK);
    }

    @PostMapping("/preview")
    @Log("预览订单")
    @ApiOperation("预览订单")
    public ResponseEntity<OrderPreviewResp> preview(@Validated @RequestBody BuyProductReq req) throws BizException {
        log.info("/api/payOrder/preview 预览订单：{}",req);
        return new ResponseEntity<>(payOrderService.preview(req), HttpStatus.OK);
    }

    @PostMapping(value = "/confirm")
    @Log("确认收货(小程序)")
    @ApiOperation("确认收货(小程序)")
    public ResponseEntity<Boolean> confirm(@RequestBody PayOrderQueryCriteria criteria) throws BizException {
        log.info("/api/payOrder/confirm 确认收货(小程序)：{}",criteria);
        return new ResponseEntity<>(payOrderService.confirm(criteria),HttpStatus.OK);
    }

    @PostMapping(value = "/record")
    @Log("录入发货信息(后管)")
    @ApiOperation("录入发货信息(后管)")
    public ResponseEntity<Boolean> record(@RequestBody RecordOrderReq req){
        log.info("/api/payOrder/record 录入发货信息(后管)：{}",req);
        return new ResponseEntity<>(payOrderService.record(req),HttpStatus.OK);
    }

    @GetMapping(value = "/detail/{orderSn}")
    @Log("订单详情（小程序）")
    @ApiOperation("订单详情（小程序）")
    public ResponseEntity<OrderDetailResp> detail(@PathVariable String orderSn) throws BizException {
        log.info("/api/payOrder/detail/{}，订单详情",orderSn);
        return new ResponseEntity<>(payOrderService.detail(orderSn),HttpStatus.OK);
    }

    @GetMapping(value = "/admin/detail/{orderSn}")
    @Log("订单详情（后管）")
    @ApiOperation("订单详情（后管）")
    public ResponseEntity<OrderDetailResp> adminDetail(@PathVariable String orderSn) throws BizException {
        log.info("/api/payOrder/admin/detail/{}，订单详情",orderSn);
        return new ResponseEntity<>(payOrderService.detail(orderSn),HttpStatus.OK);
    }

}